package cn.yikaojiuguo.student.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.ui.base.CustomerActionBarActivity;
import cn.yikaojiuguo.student.ui.view.SwitchView;

public class MessageSettingActivity extends CustomerActionBarActivity {

    @Bind(R.id.sw_sound)
    SwitchView mSw_sound;
    @Bind(R.id.sw_vibrate)
    SwitchView mSw_vibrate;

    public static Intent newIntent() {
        Intent intent = new Intent(YiKaoApplication.getInstance(),MessageSettingActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_setting);
        ButterKnife.bind(this);
        initView();
        initEvents();
    }

    private void initEvents() {
        //给该控件加了动画效果，延时200ms
        mSw_sound.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
            @Override
            public void toggleToOn(View view) {
                mSw_sound.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSw_sound.toggleSwitch(true);
                        YiKaoApplication.putMessageSoundState(true);
                    }
                },200);
            }

            @Override
            public void toggleToOff(View view) {
                mSw_sound.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSw_sound.toggleSwitch(false);
                        YiKaoApplication.putMessageSoundState(false);
                    }
                },200);
            }
        });

        mSw_vibrate.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
            @Override
            public void toggleToOn(View view) {
                mSw_vibrate.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSw_vibrate.toggleSwitch(true);
                        YiKaoApplication.putMessageVibrateState(true);
                    }
                },200);
            }

            @Override
            public void toggleToOff(View view) {
                mSw_vibrate.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSw_vibrate.toggleSwitch(false);
                        YiKaoApplication.putMessageVibrateState(false);
                    }
                },200);
            }
        });
    }

    private void initView() {
        mSw_sound.setOpened(YiKaoApplication.getMessageSoundState());
        mSw_vibrate.setOpened(YiKaoApplication.getMessageVibrateState());
    }
}
